<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>MyGallery</title>		
		<style type="text/css">
		  @import "http://dojocampus.org/dojotoolkit/dojox/image/resources/image.css";
		</style>
		<script src="http://leoner.tianji.com/dojo/dojo.js" djConfig="parseOnLoad:true, isDebug:true"></script>
		<script type="text/javascript">
			dojo.require("dojox.image.Gallery");
			dojo.require("dojo.data.ItemFileWriteStore");
			dojo.require("dojo.parser");
			dojo.addOnLoad(function(){
				coffeeStore = new dojo.data.ItemFileWriteStore({url:"cofee.json"});
				coffeeStore.onSet = function(item, attr, oldValue, newValue) {
						var itemName = coffeeStore.getValue(item, "name");
						console.log("Just modified the ", attr, "attribute for", itemName);
						console.log("length form"+oldValue.length+"to" + newValue.length);
				}
				test2();
			});
			function test2(){
				//Create a new item
				var newItem = coffeeStore.newItem({
					name : "baobei",
					description : "i love you",
					childs:[]
				});
				//Get a reference to the parent with the children
				var light;
				coffeeStore.fetchItemByIdentity({
					identity : "Light",
					onItem : function(item, request) {
						light = item;
					}
				});
				var lightChilds1 = coffeeStore.getValues(light,"childs");
				
				console.log(lightChilds1.length);
				coffeeStore.setValues(light,"childs",lightChilds1.concat(newItem));
				lightChilds1 = coffeeStore.getValues(light,"childs");
				console.log(lightChilds1.length);
				dojo.forEach(lightChilds1, function(x) {
					console.log(coffeeStore.getValue(x, "name"));
				});
			}
			function test1(){
				coffeeStore.fetchItemByIdentity({
					identity: "Spanish",
					onItem : function(item, request) {
					//capture the item...or do something with it
						spanishCoffeeItem = item;
						var value = coffeeStore.getValue(spanishCoffeeItem, "description"); //Very dark brown...
						console.log("value--->"+value);
						var a = coffeeStore.setValue(spanishCoffeeItem, "description", "El Matador...?!?");
						var description = coffeeStore.getValue(spanishCoffeeItem, "description"); // Spanish
						console.log("description--->"+description);
						//var a = coffeeStore.revert(); //revert the store.
						var item = coffeeStore.isItem(spanishCoffeeItem); //false
						console.log("is item-->"+item);
						var value1 = coffeeStore.getValue(spanishCoffeeItem, "description"); //Very dark brown...
						console.log("value1--->"+value1);
						var dirty = coffeeStore.isDirty(spanishCoffeeItem); //true
						console.log("dirty1--->"+dirty);
						coffeeStore.save();
						var dirty = coffeeStore.isDirty(spanishCoffeeItem); //true
						console.log("dirty2--->"+dirty);

						console.log("-----------------------");
						var newItem = coffeeStore.newItem({name:"kang",desc:"hui"});
						var isNewItem = coffeeStore.isItem(newItem);
						console.log("isNewItem-->"+isNewItem);
						var dirty3 = coffeeStore.isDirty(newItem); //true
						console.log("dirty3--->"+dirty3);
					},
					onError : function(item, request) {
					/* Handle the error... */
					}
					});
			}
		</script>
	</head>
	<body>

	 
	</body>
</html>
